package com.yookey.common.utils;

import com.yookey.common.constant.ResponseMessageEnum;
import com.yookey.config.MessagePropertiesConfig;

import java.text.MessageFormat;

/**
 * className ResponseMessageUtil
 *
 * @Author: Zhang Shao Hui
 * @Description:
 * @Date:Create：in 2019/11/20 13:36
 * @Modified By：
 */
public class ResponseMessageUtil {
    public static String getMessage(String key) {
        return MessagePropertiesConfig.getPropertiesMap().get(key);
    }

    public static String getMessage(ResponseMessageEnum key) {
        return MessagePropertiesConfig.getPropertiesMap().get(key.getMessageKey());
    }

    public static String getMessage(String key, String defaultValue) {
        String message = MessagePropertiesConfig.getPropertiesMap().get(key);
        if (message == null) {
            return defaultValue;
        }
        return message;
    }

    public static String getMessage(ResponseMessageEnum key, String defaultValue) {
        String message = MessagePropertiesConfig.getPropertiesMap().get(key.getMessageKey());
        if (message == null) {
            return defaultValue;
        }
        return message;
    }

    public static String getMessage(String key, Object... params) {
        String message = MessagePropertiesConfig.getPropertiesMap().get(key);
        if (params == null) {
            return message;
        }
        if (params.length < 1) {
            return message;
        }

        return MessageFormat.format(message, params);
    }

    public static String getMessage(ResponseMessageEnum key, Object... params) {
        String message = MessagePropertiesConfig.getPropertiesMap().get(key.getMessageKey());
        if (params == null) {
            return message;
        }
        if (params.length < 1) {
            return message;
        }

        return MessageFormat.format(message, params);
    }

    public static String getMessage(String key, String defaultValue, Object... params) {

        String message = MessagePropertiesConfig.getPropertiesMap().get(key);
        if (message == null) {
            return defaultValue;
        }

        if (params == null) {
            return message;
        }

        if (params.length < 1) {
            return message;
        }

        return MessageFormat.format(message, params);
    }

    public static String getMessage(ResponseMessageEnum key, String defaultValue, Object... params) {
        String message = MessagePropertiesConfig.getPropertiesMap().get(key.getMessageKey());
        if (message == null) {
            return defaultValue;
        }
        if (params == null) {
            return message;
        }

        if (params.length < 1) {
            return message;
        }
        return MessageFormat.format(message, params);
    }
}
